<?php

class Request_Http extends Request
{

	/**
	 * Return the method by which the request was made
	 *
	 * @return string
	 */
	public function getMethod()
	{
		return self::getServer('REQUEST_METHOD');
	}

	/**
	 * Retrieve an array of parameters
	 *
	 * Retrieves a merged array of parameters, with precedence of userland
	 * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the
	 * userland params will take precedence over all others).
	 *
	 * @return array
	 */
	public function getParams()
	{
		return $this -> _params + $_GET + $_POST;
	}

	/**
	 * Retrieve a parameter
	 *
	 * Retrieves a parameter from the instance. Priority is in the order of
	 * userland parameters (see {@link setParam()}), $_GET, $_POST. If a
	 * parameter matching the $key is not found, null is returned.
	 *
	 * If the $key is an alias, the actual key aliased will be used.
	 *
	 * @param mixed $key
	 * @param mixed $default Default value to use if key not found
	 * @return mixed
	 */
	public function getParam($key, $value = null)
	{
		if (method_exists($this, $method = 'get' . ucfirst($key)))
		{
			return $this -> {$method}();
		}
		else
		if (isset($this -> _params[$key]))
		{
			return $this -> _params[$key];
		}
		elseif (isset($_GET[$key]))
		{
			return $_GET[$key];
		}
		elseif (isset($_POST[$key]))
		{
			return $_POST[$key];
		}

		return $value;
	}

	/**
	 * @return string
	 */
	public function getUri()
	{
		if(null === $this->_uri){
			$uri = null;
			if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
                $uri = $_SERVER['HTTP_X_REWRITE_URL'];
            } elseif (
                // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
                isset($_SERVER['IIS_WasUrlRewritten'])
                && $_SERVER['IIS_WasUrlRewritten'] == '1'
                && isset($_SERVER['UNENCODED_URL'])
                && $_SERVER['UNENCODED_URL'] != ''
                ) {
                $uri = $_SERVER['UNENCODED_URL'];
            } elseif (isset($_SERVER['REQUEST_URI'])) {
                $uri = $_SERVER['REQUEST_URI'];
                // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
                $schemeAndHttpHost = self::getScheme() . '://' . $this->getHttpHost();
                if (strpos($uri, $schemeAndHttpHost) === 0) {
                    $uri = substr($uri, strlen($schemeAndHttpHost));
                }
            } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
                $uri = $_SERVER['ORIG_PATH_INFO'];
                if (!empty($_SERVER['QUERY_STRING'])) {
                    $uri .= '?' . $_SERVER['QUERY_STRING'];
                }
            }
			$this->_uri  = $uri;
		}
		return $this -> _uri;
	}

	public function getPath()
	{
		if (null === $this -> _path)
		{
			$baseUrl = Agis::get('baseUrl');
			$uri = $this -> getUri();
			// Remove the query string from REQUEST_URI
			if ($pos = strpos($uri, '?'))
			{
				$uri = substr($uri, 0, $pos);
			}

			if (!empty($baseUrl) && strpos($uri, $baseUrl) === 0)
			{
				$this->_path = substr($uri, strlen($baseUrl)); 
			}
			else
			{
				$this->_path = $uri;
			}
		}
		return $this -> _path;
	}


	/**
	 * @return array
	 */
	public function getGet()
	{
		return $_GET;
	}

	/**
	 * @return array
	 */
	public function getPost()
	{
		return $_POST;
	}

}
